package meconsea.life.sys.accessor;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

import javax.persistence.EntityManager;
import javax.persistence.Query;

import meconsea.life.admin.entity.Indicate;

import com.google.inject.Inject;
import com.wideplay.warp.persist.Transactional;

/**
 * <p>
 * �ò��ִ���Դ��warp-persist/example
 * </p>
 * 
 * @author meconsea MSN: meconsea@hotmail.com Mail: wangzhanhai@139.com
 * 
 * @param <T>
 * @param <PK>
 */
public class BaseAccessor<T, PK extends Serializable> implements
		IAccessor<T, PK> {

	@Inject
	protected com.google.inject.Provider<EntityManager> emp;

	private Class<T> persistentClass;

	/**
	 * Constructor for dependency injection.
	 * 
	 * @param persistentClass
	 *            the class type you'd like to persist.
	 */
	public BaseAccessor(Class<T> persistentClass) {
		this.persistentClass = persistentClass;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see org.rapin.dynf.dao.IDao#find(java.io.Serializable)
	 */
	public T find(PK id) {
		return emp.get().find(this.persistentClass, id);
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see org.rapin.dynf.dao.IDao#findAll()
	 */
	@SuppressWarnings("unchecked")
	public List<T> findAll() {
		return emp.get().createQuery(
				"FROM " + this.persistentClass.getSimpleName()).getResultList();
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see org.rapin.dynf.dao.IDao#save(java.lang.Object)
	 */
	@Transactional
	public T save(T object) {
		return emp.get().merge(object);
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see org.rapin.dynf.dao.IDao#remove(java.io.Serializable)
	 */
	@Transactional
	public void remove(PK id) {
		EntityManager em = emp.get();
		em.remove(em.find(this.persistentClass, id));
	}

	/**
	 * 通过NamedQuery来处理update
	 * 
	 * @param namedQuery
	 * @param params
	 */
	@Transactional
	public void updateNamed(String namedQuery, Object... params) {
		EntityManager em = emp.get();
		Query query = em.createNamedQuery(namedQuery);
		for (int i = 0; params != null && i < params.length; i = +2) {
			query.setParameter(params[i].toString(), params[i + 1]);
		}
		query.executeUpdate();
	}

	@Transactional
	public void update(String sql, Object... params) {
		EntityManager em = emp.get();
		Query query = em.createQuery(sql);
		for (int i = 0; params != null && i < params.length; i++) {
			query.setParameter(i + 1, params[i]);
		}
		query.executeUpdate();
	}

	/**
	 * 通过sql进行查询。
	 * 
	 * @param sql
	 * @param params
	 * @return
	 */
	public List<T> getEntitys(String sql,int first,int max, Object... params) {
		EntityManager em = emp.get();
		Query query = em.createQuery(sql);
		for (int i = 0; params != null && i < params.length; i++) {
			query.setParameter(i + 1, params[i]);
		}
		if(first>=0 && max >0){
			query.setFirstResult(first);
			query.setMaxResults(max);
		}
		System.out.println(sql);
		return query.getResultList();
	}
	
	/**
	 * 通过Named进行查询
	 * 
	 * @param namedQuery
	 * @param params
	 * @return
	 */
	public List<T> getEntitysNamed(String namedQuery,int first,int max, Object... params) {
		EntityManager em = emp.get();
		Query query = em.createNamedQuery(namedQuery);
		for (int i = 0; params != null && i < params.length; i = +2) {
			query.setParameter(params[i].toString(), params[i + 1]);
		}
		if(first>=0 && max >0){
			query.setFirstResult(first);
			query.setMaxResults(max);
		}
		return query.getResultList();
	}

}
